<?php
define("EW_PAGE_ID", "edit", TRUE); // Page ID
define("EW_TABLE_NAME", 'mobi', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "mobiinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
$Security->LoadCurrentUserLevel('mobi');
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
if (!$Security->CanEdit()) {
	$Security->SaveLastUrl();
	Page_Terminate("mobilist.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$mobi->Export = @$_GET["export"]; // Get export parameter
$sExport = $mobi->Export; // Get export parameter, used in header
$sExportFile = $mobi->TableVar; // Get export file, used in header
?>
<?php

// Load key from QueryString
if (@$_GET["m_id"] <> "") {
	$mobi->m_id->setQueryStringValue($_GET["m_id"]);
}

// Create form object
$objForm = new cFormObj();
if (@$_POST["a_edit"] <> "") {
	$mobi->CurrentAction = $_POST["a_edit"]; // Get action code
	GetUploadFiles(); // Get upload files
	LoadFormValues(); // Get form values
} else {
	$mobi->CurrentAction = "I"; // Default action is display
}

// Check if valid key
if ($mobi->m_id->CurrentValue == "") Page_Terminate($mobi->getReturnUrl()); // Invalid key, exit
switch ($mobi->CurrentAction) {
	case "I": // Get a record to display
		if (!LoadRow()) { // Load Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
			Page_Terminate($mobi->getReturnUrl()); // Return to caller
		}
		break;
	Case "U": // Update
		$mobi->SendEmail = TRUE; // Send email on update success
		if (EditRow()) { // Update Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "Update successful"; // Update success
			Page_Terminate($mobi->getReturnUrl()); // Return to caller
		} else {
			RestoreFormValues(); // Restore form values if update failed
		}
}

// Render the record
$mobi->RowType = EW_ROWTYPE_EDIT; // Render as edit
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "edit"; // Page id

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_m_icon"];
		aelm = fobj.elements["a" + infix + "_m_icon"];
		var chk_m_icon = (aelm && aelm[0])?(aelm[2].checked):true;
		if (elm && chk_m_icon && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Icon"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_m_icon"];
		if (elm && !ew_CheckFileType(elm.value)) { 
			if (!ew_OnError(elm, "File type is not allowed.")) 
				return false; 
		}
		elm = fobj.elements["x" + infix + "_m_active[]"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Active"))
				return false;
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Edit TABLE: mobi<br><br><a href="<?php echo $mobi->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
	$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
}
?>
<form name="fmobiedit" id="fmobiedit" action="mobiedit.php" method="post" enctype="multipart/form-data" onSubmit="return ew_ValidateForm(this);">
<p>
<input type="hidden" name="a_edit" id="a_edit" value="U">
<table class="ewTable">
	<tr class="ewTableRow">
		<td class="ewTableHeader">ID</td>
		<td<?php echo $mobi->m_id->CellAttributes() ?>><span id="cb_x_m_id">
<div<?php echo $mobi->m_id->ViewAttributes() ?>><?php echo $mobi->m_id->EditValue ?></div>
<input type="hidden" name="x_m_id" id="x_m_id" value="<?php echo ew_HtmlEncode($mobi->m_id->CurrentValue) ?>">
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Number</td>
		<td<?php echo $mobi->m_number->CellAttributes() ?>><span id="cb_x_m_number">
<input type="text" name="x_m_number" id="x_m_number"  size="30" maxlength="250" value="<?php echo $mobi->m_number->EditValue ?>"<?php echo $mobi->m_number->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Icon<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $mobi->m_icon->CellAttributes() ?>><span id="cb_x_m_icon">
<?php if (!is_null($mobi->m_icon->Upload->DbValue)) { ?>
<input type="radio" name="a_m_icon" id="a_m_icon" value="1" checked>Keep&nbsp;
<input type="radio" name="a_m_icon" id="a_m_icon" value="2" disabled>Remove&nbsp;
<input type="radio" name="a_m_icon" id="a_m_icon" value="3">Replace<br>
<?php } else { ?>
<input type="hidden" name="a_m_icon" id="a_m_icon" value="3">
<?php } ?>
<input type="file" name="x_m_icon" id="x_m_icon"  size="30" onchange="if (this.form.a_m_icon[2]) this.form.a_m_icon[2].checked=true;"<?php echo $mobi->m_icon->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Name</td>
		<td<?php echo $mobi->m_name->CellAttributes() ?>><span id="cb_x_m_name">
<input type="text" name="x_m_name" id="x_m_name"  size="30" maxlength="250" value="<?php echo $mobi->m_name->EditValue ?>"<?php echo $mobi->m_name->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Active<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $mobi->m_active->CellAttributes() ?>><span id="cb_x_m_active">
<?php
$arwrk = $mobi->m_active->EditValue;
if (is_array($arwrk)) {
	$armultiwrk= explode(",", strval($mobi->m_active->CurrentValue));
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = "";
		for ($ari = 0; $ari < count($armultiwrk); $ari++) {
			if (strval($arwrk[$rowcntwrk][0]) == trim(strval($armultiwrk[$ari]))) {
				$selwrk = " checked";
				break;
			}
		}
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="checkbox" name="x_m_active[]" id="x_m_active[]" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php echo $mobi->m_active->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
	</tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="   Edit   ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Function Get upload files
function GetUploadFiles() {
	global $objForm, $mobi;

	// Get upload data
		if ($mobi->m_icon->Upload->UploadFile()) {

			// No action required
		} else {
			echo $mobi->m_icon->Upload->Message;
			exit();
		}
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $mobi;
	$mobi->m_id->setFormValue($objForm->GetValue("x_m_id"));
	$mobi->m_number->setFormValue($objForm->GetValue("x_m_number"));
	$mobi->m_name->setFormValue($objForm->GetValue("x_m_name"));
	$mobi->m_active->setFormValue($objForm->GetValue("x_m_active"));
}

// Restore form values
function RestoreFormValues() {
	global $mobi;
	$mobi->m_id->CurrentValue = $mobi->m_id->FormValue;
	$mobi->m_number->CurrentValue = $mobi->m_number->FormValue;
	$mobi->m_name->CurrentValue = $mobi->m_name->FormValue;
	$mobi->m_active->CurrentValue = $mobi->m_active->FormValue;
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $mobi;
	$sFilter = $mobi->SqlKeyFilter();
	if (!is_numeric($mobi->m_id->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@m_id@", ew_AdjustSql($mobi->m_id->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$mobi->Row_Selecting($sFilter);

	// Load sql based on filter
	$mobi->CurrentFilter = $sFilter;
	$sSql = $mobi->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$mobi->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $mobi;
	$mobi->m_id->setDbValue($rs->fields('m_id'));
	$mobi->m_number->setDbValue($rs->fields('m_number'));
	$mobi->m_icon->Upload->DbValue = $rs->fields('m_icon');
	$mobi->m_name->setDbValue($rs->fields('m_name'));
	$mobi->m_active->setDbValue($rs->fields('m_active'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $mobi;

	// Call Row Rendering event
	$mobi->Row_Rendering();

	// Common render codes for all row types
	// m_id

	$mobi->m_id->CellCssStyle = "";
	$mobi->m_id->CellCssClass = "";

	// m_number
	$mobi->m_number->CellCssStyle = "";
	$mobi->m_number->CellCssClass = "";

	// m_icon
	$mobi->m_icon->CellCssStyle = "";
	$mobi->m_icon->CellCssClass = "";

	// m_name
	$mobi->m_name->CellCssStyle = "";
	$mobi->m_name->CellCssClass = "";

	// m_active
	$mobi->m_active->CellCssStyle = "";
	$mobi->m_active->CellCssClass = "";
	if ($mobi->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($mobi->RowType == EW_ROWTYPE_ADD) { // Add row
	} elseif ($mobi->RowType == EW_ROWTYPE_EDIT) { // Edit row

		// m_id
		$mobi->m_id->EditCustomAttributes = "";
		$mobi->m_id->EditValue = $mobi->m_id->CurrentValue;
		$mobi->m_id->CssStyle = "";
		$mobi->m_id->CssClass = "";
		$mobi->m_id->ViewCustomAttributes = "";

		// m_number
		$mobi->m_number->EditCustomAttributes = "";
		$mobi->m_number->EditValue = ew_HtmlEncode($mobi->m_number->CurrentValue);

		// m_icon
		$mobi->m_icon->EditCustomAttributes = "";
		$mobi->m_icon->EditValue = $mobi->m_icon->CurrentValue;

		// m_name
		$mobi->m_name->EditCustomAttributes = "";
		$mobi->m_name->EditValue = ew_HtmlEncode($mobi->m_name->CurrentValue);

		// m_active
		$mobi->m_active->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("1", "Active");
		$mobi->m_active->EditValue = $arwrk;
	} elseif ($mobi->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$mobi->Row_Rendered();
}
?>
<?php

// Update record based on key values
function EditRow() {
	global $conn, $Security, $mobi;
	$sFilter = $mobi->SqlKeyFilter();
	if (!is_numeric($mobi->m_id->CurrentValue)) {
		return FALSE;
	}
	$sFilter = str_replace("@m_id@", ew_AdjustSql($mobi->m_id->CurrentValue), $sFilter); // Replace key value
	$mobi->CurrentFilter = $sFilter;
	$sSql = $mobi->SQL();
	$conn->raiseErrorFn = 'ew_ErrorFn';
	$rs = $conn->Execute($sSql);
	$conn->raiseErrorFn = '';
	if ($rs === FALSE)
		return FALSE;
	if ($rs->EOF) {
		$EditRow = FALSE; // Update Failed
	} else {

		// Save old values
		$rsold =& $rs->fields;
		$rsnew = array();

		// Field m_id
		// Field m_number

		$mobi->m_number->SetDbValueDef($mobi->m_number->CurrentValue, NULL);
		$rsnew['m_number'] =& $mobi->m_number->DbValue;

		// Field m_icon
		$mobi->m_icon->Upload->SaveToSession(); // Save file value to Session
		if ($mobi->m_icon->Upload->Action == "2" || $mobi->m_icon->Upload->Action == "3") { // Update/Remove
		$mobi->m_icon->DbValue = $rs->fields('m_icon'); // Get original value
		if (is_null($mobi->m_icon->Upload->Value)) {
			$mobi->m_icon->Upload->DbValue = NULL;
		} else {
			$mobi->m_icon->Upload->DbValue = ew_UploadFileNameEx(ew_UploadPathEx(TRUE, EW_UPLOAD_DEST_PATH), $mobi->m_icon->Upload->FileName);
		}
		$rsnew['m_icon'] =& $mobi->m_icon->Upload->DbValue;
		}

		// Field m_name
		$mobi->m_name->SetDbValueDef($mobi->m_name->CurrentValue, NULL);
		$rsnew['m_name'] =& $mobi->m_name->DbValue;

		// Field m_active
		$mobi->m_active->SetDbValueDef($mobi->m_active->CurrentValue, 0);
		$rsnew['m_active'] =& $mobi->m_active->DbValue;

		// Call Row Updating event
		$bUpdateRow = $mobi->Row_Updating($rsold, $rsnew);
		if ($bUpdateRow) {

		// Field m_icon
			if (!is_null($mobi->m_icon->Upload->Value)) ew_SaveFile(ew_UploadPathEx(TRUE, EW_UPLOAD_DEST_PATH), $rsnew['m_icon'], $mobi->m_icon->Upload->Value);
			$conn->raiseErrorFn = 'ew_ErrorFn';
			$EditRow = $conn->Execute($mobi->UpdateSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($mobi->CancelMessage <> "") {
				$_SESSION[EW_SESSION_MESSAGE] = $mobi->CancelMessage;
				$mobi->CancelMessage = "";
			} else {
				$_SESSION[EW_SESSION_MESSAGE] = "Update cancelled";
			}
			$EditRow = FALSE;
		}
	}

	// Call Row Updated event
	if ($EditRow) {
		$mobi->Row_Updated($rsold, $rsnew);
	}
	$rs->Close();

	// Field m_icon
	$mobi->m_icon->Upload->RemoveFromSession(); // Remove file value from Session
	return $EditRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
